//
//  NewFeatureViewController.swift
//  JJWeibo
//
//  Created by JJ on 15/9/5.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    
    private let JJNewFeatureCount=4
    
    init(){
        super.init(collectionViewLayout:UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()

       
        self.collectionView!.registerClass(JJNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        prepareLayout()
    }

   private func prepareLayout(){
        
        let layout=collectionView?.collectionViewLayout as!UICollectionViewFlowLayout
        layout.itemSize=view.bounds.size
        layout.minimumInteritemSpacing=0
        layout.minimumLineSpacing=0
        layout.scrollDirection=UICollectionViewScrollDirection.Horizontal

        collectionView?.pagingEnabled=true
        collectionView?.showsHorizontalScrollIndicator=false
        collectionView?.bounces=false
    
    }
 
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      
        return JJNewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as!JJNewFeatureCell
        
        cell.imageIndex=indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path=collectionView.indexPathsForVisibleItems().last!
        if path.item == JJNewFeatureCount-1{
            let cell=collectionView.cellForItemAtIndexPath(path) as! JJNewFeatureCell
            cell.showBtn()
        }
        
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    private class JJNewFeatureCell:UICollectionViewCell {
        
        private var imageIndex:Int=0{
            didSet{
                iconView.image=UIImage(named: "new_feature_\(imageIndex+1)")
                clickBtn.hidden=true
            }
        }
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUI()
        }

        required init?(coder aDecoder: NSCoder) {
           super.init(coder: aDecoder)
            setUI()
        }
        
        private func setUI(){
            addSubview(iconView)
            addSubview(clickBtn)
            
            iconView.frame=bounds
            clickBtn.translatesAutoresizingMaskIntoConstraints=false
            addConstraint(NSLayoutConstraint(item: clickBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: clickBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
        }
        
        private lazy var iconView=UIImageView()
        
        private lazy var clickBtn:UIButton={
            let Btn=UIButton()
            Btn.setTitle("开始体验", forState: UIControlState.Normal)
            Btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
            Btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
            Btn.sizeToFit()
            Btn.addTarget(self, action: "start", forControlEvents: UIControlEvents.TouchUpInside)
            return Btn
        }()
        
       @objc private func start(){
            NSNotificationCenter.defaultCenter().postNotificationName(JJSwitchRootViewControllerNotification, object: nil)
        }
        
        private func showBtn(){
            clickBtn.hidden=false
            clickBtn.transform=CGAffineTransformMakeScale(0, 0)
            
            UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
                self.clickBtn.transform=CGAffineTransformIdentity
                }) { (_) -> Void in
    
            }
        }
    }
}













